package top.zywork.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zywork.constant.CommonConstant;
import top.zywork.dao.GraphicOrderDAO;
import top.zywork.dos.GraphicOrderDO;
import top.zywork.dos.SchoolDO;
import top.zywork.dto.GraphicOrderDTO;
import top.zywork.enums.PayStatusEnum;
import top.zywork.ext.dos.GraphicExtDO;
import top.zywork.ext.dos.GraphicLinkExtDO;
import top.zywork.ext.dos.GraphicOrderExtDO;
import top.zywork.ext.mapper.GraphicOrderExtMapper;
import top.zywork.ext.service.GraphicExtService;
import top.zywork.ext.service.GraphicLinkExtService;
import top.zywork.security.SecurityUtils;
import top.zywork.service.AbstractBaseService;
import top.zywork.service.GraphicOrderService;
import top.zywork.service.SchoolService;
import top.zywork.utils.ODDGenerator;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.wx.save.GraphicOrderSave;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;

/**
 * GraphicOrderServiceImpl服务接口实现类<br/>
 * <p>
 * 创建于2020-11-28<br/>
 *
 * @author http://ljyanimo.com 刘金泳
 * @version 1.0
 */
@Service(value = "graphicOrderService")
public class GraphicOrderServiceImpl extends AbstractBaseService implements GraphicOrderService {

    private GraphicOrderDAO graphicOrderDAO;

    /**
     * 学校
     */
    @Autowired
    private SchoolService schoolService;

    @Autowired
    private GraphicExtService graphicExtService;

    @Autowired
    private GraphicLinkExtService graphicLinkExtService;

    @Resource
    private GraphicOrderExtMapper graphicOrderExtMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResponseStatusVO placeOrder(GraphicOrderSave graphicOrderSave) {
        // 生成订单号
        String orderId = ODDGenerator.getC();

        long userId = SecurityUtils.getJwtUser().getUserId();

        // 检查学校是否存在
        SchoolDO schoolDO = schoolService.checkSchool(graphicOrderSave.getSchoolId());
        if (schoolDO == null) {
            return ResponseStatusVO.error("学校不存在", null);
        }

        // 检查图文店
        GraphicExtDO graphicExtDO = graphicExtService.getById(graphicOrderSave.getGraphicId());
        if (graphicExtDO == null) {
            return ResponseStatusVO.error("图文店不存在", null);
        }

        // 规格链
        GraphicLinkExtDO graphicLinkExtDO = graphicLinkExtService.getById(graphicOrderSave.getSpecId());

        GraphicOrderExtDO graphicOrderExtDO = new GraphicOrderExtDO();
        // 订单id
        graphicOrderExtDO.setOrderId(orderId);
        // 下单用户
        graphicOrderExtDO.setUserId(userId);
        // 图文店id
        graphicOrderExtDO.setGraphicId(graphicExtDO.getId());
        // 学校ID
        graphicOrderExtDO.setSchoolId(graphicOrderSave.getSchoolId());
        // 文件地址
        graphicOrderExtDO.setUploadFile(graphicOrderSave.getUploadFile());

        // 页码范围
        graphicOrderExtDO.setYmfw(graphicOrderSave.getYmfw());

        // 默认是未激活
        graphicOrderExtDO.setIsActive(CommonConstant.IS_ACTIVE_FALSE);

        // 规格类型
        graphicOrderExtDO.setZzlx(graphicLinkExtDO.getZzlx());
        graphicOrderExtDO.setYs(graphicLinkExtDO.getYs());
        graphicOrderExtDO.setDsm(graphicLinkExtDO.getDsm());
        graphicOrderExtDO.setSuoyin(graphicOrderSave.getSuoyin());
        graphicOrderExtDO.setZhuangding(graphicOrderSave.getZhuangding());

        // 打印数量
        graphicOrderExtDO.setDysl(graphicOrderSave.getDysl());

        // 支付状态
        graphicOrderExtDO.setPayStatus(PayStatusEnum.TO_BE_PAID.getCode());

        // 备注
        graphicOrderExtDO.setBeizhu(graphicOrderSave.getBeizhu());

        // 打印状态
        graphicOrderExtDO.setCompletionStatus("未打印");

        BigDecimal totalAmount;


        // 单双面涉及到数量 大小  dsm.getTotalAmount()
        String ymfw = graphicOrderSave.getYmfw();
        String[] ymfws = ymfw.split("-");
        if (ymfws.length != 2) {
            return ResponseStatusVO.error("页码范围异常", null);
        }
        Integer one = Integer.valueOf(ymfws[0]);
        Integer two = Integer.valueOf(ymfws[1]);

        Integer ym = two - one <= 0 ? 1 : (two - one) + 1;

        // TODO 暂无考虑单面双面的计算
        BigDecimal ymbig = new BigDecimal(ym);

        // 页码范围乘以规格
        BigDecimal yf = ymbig.multiply(graphicLinkExtDO.getPrice());

        totalAmount = yf.multiply(BigDecimal.valueOf(graphicOrderSave.getDysl()));

        // 设置金额
        graphicOrderExtDO.setTotalAmount(totalAmount);
        graphicOrderExtDO.setPayAmount(totalAmount);
        graphicOrderExtDO.setDiscountAmount(BigDecimal.ZERO);

        // 订单创建时间
        graphicOrderExtDO.setCreateTime(new Date());

        graphicOrderExtMapper.insert(graphicOrderExtDO);

        return ResponseStatusVO.ok("订单保存成功", orderId);
    }

    @Autowired
    public void setGraphicOrderDAO(GraphicOrderDAO graphicOrderDAO) {
        super.setBaseDAO(graphicOrderDAO);
        this.graphicOrderDAO = graphicOrderDAO;
    }

    @PostConstruct
    public void init() {
        super.init(GraphicOrderDO.class, GraphicOrderDTO.class);
    }

    public static void main(String[] args) {
        String now = LocalDate.now().toString();
        String time = String.valueOf(System.currentTimeMillis());
        System.out.println(now.substring(2).replace("-","") + "-" + time.substring(time.length() - 4));
    }
}
